package com.xujianlong.day06;

import com.xujianlong.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.types.ListValue;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Flink02_KeyedState_ListState {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.将数据转为JavaBean
        SingleOutputStreamOperator<WaterSensor> map = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //4.对相同key的数据做聚合
        KeyedStream<WaterSensor, String> keyedStream = map.keyBy(WaterSensor::getId);

        keyedStream.process(new KeyedProcessFunction<String, WaterSensor, String>() {
            private ListState<Integer> listState;

            @Override
            public void open(Configuration parameters) throws Exception {
                listState=getRuntimeContext().getListState(new ListStateDescriptor<Integer>("list_state",Integer.class));
            }

            @Override
            public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                Iterable<Integer> integers = listState.get();
                ArrayList<Integer> tmpList = new ArrayList<>();
                for (Integer integer : integers) {
                    tmpList.add(integer);
                }

                tmpList.sort(new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o2-o1;
                    }
                });

                if(tmpList.size()>3){
                    tmpList.remove(3);
                }
                listState.update(tmpList);
                out.collect(tmpList.toString());

            }
        }).print();
        //mmmmm
            keyedStream.process(new KeyedProcessFunction<String, WaterSensor, String>() {
                private ListState<Integer> listState;
                @Override
                public void open(Configuration parameters) throws Exception {
                    listState=getRuntimeContext().getListState(new ListStateDescriptor<Integer>("list_date",Integer.class));
                }

                @Override
                public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                    Iterable<Integer> integers = listState.get();
                    ArrayList<Integer> tmpList=new ArrayList<>();

                    for (Integer integer : integers) {
                        tmpList.add(integer);
                    }

                    tmpList.sort(new Comparator<Integer>() {
                        @Override
                        public int compare(Integer o1, Integer o2) {
                            return o2-o1;
                        }
                    });

                    if(tmpList.size()>3){
                        tmpList.remove(3);
                    }
                    listState.update(tmpList);

                    out.collect(tmpList.toString());
                }
            });
        //mmmmm
        env.execute();
    }
}
